package com.example.youjingjing.electronicbalance.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;


import com.example.youjingjing.electronicbalance.presenter.BasePresenter;
import com.example.youjingjing.electronicbalance.presenter.BasePresenterIml;
import com.example.youjingjing.electronicbalance.view.BaseView;

import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BaseFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BaseFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public abstract class BaseFragment<V extends BaseView, P extends BasePresenterIml<V>> extends Fragment implements BaseView{
    protected P presenter;
    private View baseLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (baseLayout == null) {
            baseLayout = createView(inflater, container);
            ButterKnife.bind(this, baseLayout);
            //创建presenter
            presenter = createPresenter();
            //内存泄露
            //关联View
            if (presenter != null)
                presenter.attachView((V) this);//presenter取得与界面的联系
        }  else{
            ViewGroup parent = (ViewGroup) container.getParent();
            parent.removeView(baseLayout);
        }
        // Inflate the layout for this fragment
        return baseLayout;

    }
    protected abstract View createView(LayoutInflater inflater, ViewGroup container);
    protected abstract P createPresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView(true);
    }

    @Override
    public void jumpActivity(Class<?> cls, Bundle bundle) {

    }

    @Override
    public void jumpActivity(Class<?> cls) {

    }

    @Override
    public void hideKeyBoard() {
        View view = getActivity().getCurrentFocus();
        if(view!=null){
            ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
